package com.yang.controller.common;

import com.yang.dto.Authorization;
import com.yang.service.UserService;
import com.yang.utils.ThreadLocalUtil;
import com.yang.vo.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@Slf4j
@Api(tags = "安全校验控制层")
@RestController
public class SecurityVerifyController {

    @Autowired
    private UserService userService;


    @ApiOperation("注册")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody Authorization authorization){
        log.info("注册新用户:\n{}",authorization);
        return userService.register(authorization);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody Authorization authorization){
        log.info("用户登陆中:\n{}",authorization);
        return userService.login(authorization);
    }

    @ApiOperation("退出")
    @GetMapping("/logout")
    public ResponseResult logout(){
        log.info("id为{}的用户退出登录",ThreadLocalUtil.get());
        return userService.logout();
    }

    @ApiOperation("更改密码")
    @PutMapping("/password")
    public ResponseResult updatePassword(@RequestBody Authorization authorization){
        log.info("用户:\n{}修改密码:{}",ThreadLocalUtil.get(),authorization);
        return userService.updatePassword(authorization);
    }

    @ApiOperation("设置或更新密保")
    @PutMapping("/security")
    public ResponseResult setOrUpdateSecurity(@RequestBody Authorization authorization){
        log.info("用户:\n{}设置了密保：{}", ThreadLocalUtil.get(),authorization);
        return userService.setOrUpdateSecurity(authorization);
    }

    @ApiOperation("获取密保问题")
    @GetMapping("/security")
    public ResponseResult getSecurity(){
        log.info("id为{}的用户获取了密保问题",ThreadLocalUtil.get());
        return userService.getSecurity();
    }

}
